/*
 * @Author: 码农 2646995216@qq.com
 * @Date: 2025-03-07 15:57:40
 * @LastEditors: 码农 2646995216@qq.com
 * @LastEditTime: 2025-03-07 21:40:23
 * @FilePath: /pthread/RoundQueue/text.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include"RoundQueue.hpp"
#include"Task.hpp"
#include<pthread.h>
#include<ctime>
#include<unistd.h>

using namespace  std;

//生产者
void* producer(void* args)
{
	
	srand(time(nullptr));
	int len=oper.size();
	RoundQwueue<Task>* td=static_cast<RoundQwueue<Task>*>(args);
	
    while(true)
	{
		
		int data1=rand()%10;
		usleep(100);
		int data2=rand()%5;
		char op=oper[rand()%len];
		
		Task t(data1,data2,op);
		td->push(t);

        cout<<"producer give a task:"<<t.GetTask()<<endl;
		sleep(1);
	}
}

//消费者
void* consumer(void* args)
{
	RoundQwueue<Task>* td=static_cast<RoundQwueue<Task>*>(args);
	while(true)
	{
		Task t;
		td->pop(t);
		t.run();
    	
		cout<<"consumer get a task:"<<t.GetResult()<<endl;
		
	}
}


int main()
{
	RoundQwueue<Task>* tr=new RoundQwueue<Task>;//创造队列
	
	pthread_t tid1;
	pthread_t tid2;
	
	pthread_create(&tid1,nullptr,producer,(void*)tr);
	pthread_create(&tid2,nullptr,consumer,(void*)tr);
	
	pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
	
	delete tr;
	return 0;
}
